package com.dsj.gxrd.modules.people.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsj.gxrd.modules.people.domain.dto.RdUserInfoDto;
import com.dsj.gxrd.modules.people.domain.entity.RdUserInfo;
import com.dsj.gxrd.modules.people.domain.query.RdUserInfoQuery;
import com.dsj.gxrd.modules.people.domain.vo.RdUserInfoVo;
import com.dsj.gxrd.modules.people.mapper.RdUserInfoMapper;
import com.dsj.gxrd.modules.people.service.RdUserInfoService;
import com.dsj.gxrd.utils.BaseEntityHandlerUtils;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteAuthService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.form.RegisterBody;
import com.ruoyi.system.api.model.LoginUser;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author songpin566
 * @date 2023/09/21
 **/
@Service
public class RdUserInfoServiceImpl extends ServiceImpl<RdUserInfoMapper,RdUserInfo> implements RdUserInfoService {

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private RemoteAuthService authService;


    @Override
    public List<RdUserInfoVo> list(RdUserInfoQuery userInfoQuery) {
        String phonenumber = userInfoQuery.getPhonenumber();
        String name = userInfoQuery.getName();
        List<RdUserInfo> list = this.lambdaQuery()
                .eq(StringUtils.isNotEmpty(phonenumber), RdUserInfo::getPhonenumber, phonenumber)
                .like(StringUtils.isNotEmpty(name), RdUserInfo::getName, name)
                .list();
        return null;
    }

    @DS("master")
    @Override
    @Transactional(rollbackFor = Exception.class)
    @GlobalTransactional
    public AjaxResult add(RdUserInfoDto userInfoDto) {
        //TODO 需要解决后面save(rdUserInfo)报错想回滚事务，但是之前的authService.register()事务回滚不了问题
        //查询对应的userId
        R<?> register = authService.register(new RegisterBody(userInfoDto.getPhonenumber(), userInfoDto.getPhonenumber()), SecurityConstants.INNER);
        if (Constants.FAIL.equals(register.getCode())) {
            //注册失败
            return AjaxResult.error(register.getMsg());
        }
        String phonenumber = userInfoDto.getPhonenumber();
        R<LoginUser> r = remoteUserService.getUserInfo(phonenumber, SecurityConstants.INNER);
        if (r.getCode() == Constants.FAIL) {
            return AjaxResult.error("输入的用户名不存在!");
        }
        LoginUser registerUser = r.getData();
        //给用户加角色 中间表添加数据
        //List<Long> roleIds = userInfoDto.getRoleIds();
        //TODO 用户的角色权限待完善,初始用户还未设置权限
            RdUserInfo rdUserInfo = BeanUtils.copyProperties(userInfoDto, RdUserInfo.class);
        BaseEntityHandlerUtils.fastSetBaseEntity(rdUserInfo, SecurityUtils.getLoginUser());
        rdUserInfo.setUserId(registerUser.getSysUser().getUserId());
        rdUserInfo.setDelFlag(0);
        rdUserInfo.setStatus(0);
        return save(rdUserInfo) ? AjaxResult.success() : AjaxResult.error("新增机构人员失败");
    }

    @Override
    public List<RdUserInfoVo> test01() {

        return null;
    }

}
